#include "stdio.h"
#include "SDL/SDL.h"
int main(int argc, char *argv[])
{
    int done = 1;
    SDL_Surface *screen;	//This pointer will reference the backbuffer
    SDL_Surface *image1,*image2,*image3,*image4,*image5;	//This pointer will reference our bitmap sprite
    Uint8 *keystate;
    SDL_Surface *temp;//This pointer will temporarily reference our bitmap 	sprite
    SDL_Rect src, dest;//These rectangles will describe the source and 	destination regions of our blit
    //We must first initialize the SDL video component, and check for success
    SDL_Event event;
    int i=0;
    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }
    //Set the video mode to fullscreen 640x480 with 16bit colour and double-	buffering
    screen = SDL_SetVideoMode(900, 900, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    if (screen == NULL)
    {
        printf("Unable to set video mode: %s\n", SDL_GetError());
        return 1;
    }
    /*temp = SDL_LoadBMP("img/menu1.bmp");
    if (image == NULL) {
    	printf("Unable to load bitmap: %s\n", SDL_GetError());
    	return 1;
    }*/
    //Load the bitmap into a temporary surface, and check for success
    image1 = SDL_LoadBMP("img/image1.bmp");
    if (image1 == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    image2 = SDL_LoadBMP("img/image2.bmp");
    if (image2 == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    image3 = SDL_LoadBMP("img/image3.bmp");
    if (image3 == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
    image4 = SDL_LoadBMP("img/image4.bmp");
    if (image4 == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
    image5 = SDL_LoadBMP("img/image5.bmp");
    if (image5 == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
    //Convert the surface to the appropriate display format



    //image = SDL_DisplayFormat(temp);
    //Release the temporary surface
    //SDL_FreeSurface(temp);
    //Construct the source rectangle for our blit
    src.x = 0;
    src.y = 0;
    src.w = image1->w;	//Use image->w to display the entire width of the image
    src.h = image1->h;	//Use image->h to display the entire height of the image
    //Construct the destination rectangle for our blit
    dest.x = 0;//Display the image at the (X,Y) coordinates (100,100)
    dest.y = 0;
    dest.w = image1->w;
    dest.h = image1->h;
    SDL_BlitSurface(image1, &src, screen, &dest);
    while(done)
    {
        //SDL_BlitSurface(temp, &src, screen, &dest);
//SDL_UpdateRect(screen, 0, 0, 0, 0);

        while (SDL_PollEvent(&event))
        {
            // check for messages
            switch (event.type)
            {
                // exit if the window is closed
            case SDL_QUIT:
                done = 0;
                break;
                // check for keypresses
            case SDL_KEYDOWN:
            {
                // exit if ESCAPE is pressed
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    done = 0;
                if (event.key.keysym.sym == SDLK_DOWN)
                    i++;

                if (event.key.keysym.sym == SDLK_v)
                    done= 0;
                i=i%5;
                break;
            }
            } // end switch
            if(i == 0)
                //Blit the image to the backbuffer
                SDL_BlitSurface(image1, NULL, screen, NULL);
            if(i == 1)
                //Blit the image to the backbuffer
                SDL_BlitSurface(image2, NULL, screen, NULL);
            if(i == 2)
                //Blit the image to the backbuffer
                SDL_BlitSurface(image3, NULL, screen, NULL);
            if(i == 3)
                //Blit the image to the backbuffer
                SDL_BlitSurface(image4, NULL, screen, NULL);
            if(i == 4)
                //Blit the image to the backbuffer
                SDL_BlitSurface(image5, NULL, screen, NULL);

            /*	keystate = SDL_GetKeyState(NULL);
             if (keystate[SDLK_LEFT] ) {
            temp = SDL_LoadBMP("img/menu2.bmp");

            }
            if (keystate[SDLK_RIGHT] ) {
            temp = SDL_LoadBMP("img/menu3.bmp");
            }
            if (keystate[SDLK_UP] ) {
            temp = SDL_LoadBMP("img/menu4.bmp");
            }
            if (keystate[SDLK_DOWN] ) {
            temp = SDL_LoadBMP("img/menu5.bmp");
            }
             if ( dest.x > 698 ) {
              dest.x = 698;
            }
            if ( dest.y > 243 ) {
              dest.x = 243;
            }
            */

            SDL_Flip(screen);
        } // end of message processing

    }

    //Release the surface
    SDL_FreeSurface(image1);
    SDL_FreeSurface(image2);
    SDL_FreeSurface(image3);
    SDL_FreeSurface(image4);
    SDL_FreeSurface(image5);
    SDL_FreeSurface(temp);

    //Return success!
    return 0;

}
